package com.lanou3g.zjl.chating.home.chatwindow;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;
import com.lanou3g.zjl.chating.home.HomeActivity;
import com.lanou3g.zjl.chating.home.message.MessageFragment;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 15/11/20.
 */
public class ChatWindowActivity extends BaseActivity {
    public static ChatWindowActivity activityInstance; // 聊天窗口的单例
    private String userName; // 聊天对象
    private int chatType; // 会话类型
    private ChatWindowFragment chatFragment;


    @Override
    protected int getLayout() {
        return R.layout.activity_chatwindow;
    }

    @Override
    protected void initData() {
        activityInstance = this;
        // 从上个页面得到聊天对象
        userName = getIntent().getExtras().getString("username");
        chatType = getIntent().getExtras().getInt("chattype");
        String groupName = null;
        String groupId = null;
//        try {
            groupName = getIntent().getExtras().getString("groupName");
            groupId = getIntent().getExtras().getString("groupId");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        // 加载新的Fragment
        chatFragment = new ChatWindowFragment();
        Bundle args = new Bundle();
        args.putString("username", userName);
        args.putInt("chattype", chatType);
        args.putString("groupId", groupId);
        args.putString("groupName", groupName);
//        try {
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        chatFragment.setArguments(args);
        // 传入参数
//        chatFragment.setArguments(getIntent().getExtras());
        // 加载fragment
        getSupportFragmentManager().beginTransaction().add(R.id.chatWindow_Frm, chatFragment).commit();
    }

    /**
     * 执行顺序在 onRestart，onStart，onResume 之前
     * 保证只有一个聊天界面
     */
    @Override
    protected void onNewIntent(Intent intent) {
        String userId = intent.getStringExtra("username");
        if (userName.equals(userId)) {
            super.onNewIntent(intent);
        } else {
            finish();
            startActivity(intent);
        }
    }

    @Override
    public void onBackPressed() {
        switch (chatFragment.menuState()) {
            case 0:
                chatFragment.menuAppear();

                break;
            case 1:
                chatFragment.expressionMenu();

                break;
            case 2:
                double d = 1.00;
                EventBus.getDefault().post(d);
                super.onBackPressed();
        }
    }
}
